package com.mijie.core;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.ibatis.session.ResultHandler;
import org.mybatis.spring.SqlSessionTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;


@Repository
public class IBatisDao {
	
	@Autowired
	private SqlSessionTemplate sqlSession;
	
	protected <T> T selectOne(String statement){
		return sqlSession.selectOne(statement);
	}
	
	protected String selectStr(String statement){
		return sqlSession.selectOne(statement);
	}
	
	protected Long selectLong(String statement){
		return sqlSession.selectOne(statement);
	}
	
	protected Integer selectInteger(String statement){
		return sqlSession.selectOne(statement);
	}
	
	protected <T> T selectOne(String statement,Object parameter){
		return sqlSession.selectOne(statement, parameter);
	}
	
	
	protected String selectStr(String statement,Object parameter){
		return sqlSession.selectOne(statement, parameter);
	}
	
	protected Long selectLong(String statement,Object parameter){
		return sqlSession.selectOne(statement, parameter);
	}
	
	protected Integer selectInteger(String statement,Object parameter){
		return sqlSession.selectOne(statement, parameter);
	}
	
	protected <T> List<T> selectList(String statement){
		return sqlSession.selectList(statement);
	}
	
	protected <K,V> Map<K, V> selectMap(String statement,Object parameter,String mapKey){
		return sqlSession.selectMap(statement, parameter, mapKey);
	}
	
	
	protected List<String> selectStrList(String statement){
		return sqlSession.selectList(statement);
	}
	
	protected List<Long> selectLongList(String statement){
		return sqlSession.selectList(statement);
	}
	
	protected List<Integer> selectIntList(String statement){
		return sqlSession.selectList(statement);
	}
	
	protected <T> List<T> selectList(String statement,Object parameter){
		return sqlSession.selectList(statement,parameter);
	}
	
	
	protected List<String> selectStrList(String statement,Object parameter){
		return sqlSession.selectList(statement,parameter);
	}
	
	protected List<Long> selectLongList(String statement,Object parameter){
		return sqlSession.selectList(statement,parameter);
	}
	
	protected List<Integer> selectIntList(String statement,Object parameter){
		return sqlSession.selectList(statement,parameter);
	}
	
	protected int insert(String statement){
		return sqlSession.insert(statement);
	}
	
	protected int insert(String statement,Object parameter){
		return sqlSession.insert(statement,parameter);
	}
	
	protected int update(String statement){
		return sqlSession.update(statement);
	}
	
	protected int update(String statement,Object parameter){
		return sqlSession.update(statement,parameter);
	}
	
	protected int delete(String statement){
		return sqlSession.delete(statement);
	}
	
	protected int delete(String statement,Object parameter){
		return sqlSession.delete(statement,parameter);
	}
	
	protected <T> Paging<T> selectPage(Paging<T> paging,String statement,Map<String,Object> parameter){
		return selectPage(paging,statement+"_list",statement+"_count",parameter);
	}
	
	protected <T> Paging<T> selectPage(Paging<T> paging,String statement,String countStatement,Map<String,Object> parameter){
		if(paging==null) paging = new Paging<T>();
		if(parameter==null) parameter = new HashMap<String,Object>(1);
		Long totalRow = sqlSession.selectOne(countStatement, parameter);
		if(totalRow==null || totalRow.equals(0)){
			paging.reset();
		}else{
			int pageCount =(int) (totalRow / paging.getPageSize());
			if(totalRow % paging.getPageSize() != 0) pageCount+=1;
			paging.setPageCount(pageCount);
			paging.setRows(totalRow);
			if(paging.getPageNum()>0){
				paging.setOffset(paging.getPageSize() * (paging.getPageNum()-1));
			}
			parameter.put("offset", paging.getOffset());
			parameter.put("limit", paging.getPageSize());
			List<T> list = sqlSession.selectList(statement,parameter);
			
			paging.setList(list);
			paging.setOffset(paging.getOffset()+list.size());
			if(paging.getPageNum()<=0 && !list.isEmpty()) paging.setPageNum(1);
		}
		
		return paging;
	}
	
}
